﻿using System;
using System.Collections.Generic;
using System.Text;
using SH;

namespace MTS
{
	using mtMsgResponse = mtMsgRequest;

	internal class mtSMSServerCallback:mtOuterface<ImtSMSServerCallback>
    {
		public mtSMSServerCallback()
		{
			m_interfaceName = "mtSMSServerCallback";
			m_nClassID = ImtSMSServerCallback._classID;
		}

		protected override shErrorCode OnRequest(int nClassID, int nFuncID, string token, ref MTSSerializer sc)
        {
            shErrorCode ec = shErrorCode.sh_ec_NoInterface;

			switch (nFuncID)
			{

				case ImtSMSServerCallback.SMSServerLogOn:
                    {
						mtLogOnInfo info = new mtLogOnInfo();

						sc.serialize(info);

						mtCredential crd;

						ec = m_pCallback.OnLogOn(info, out crd);

						if(ec == shErrorCode.sh_ec_Success)
                        {
							//	m_pHost.RunEnv.Credential = crd;

							m_pHost.BindStub( crd,info,null);

							sc = new MTSSerializer();

							sc.serialize(crd);
                        }

					}
					break;
				case ImtSMSServerCallback.SMSServerLogOut:
					{
						var IvkParam = mtCHelptr.MakeInvokeParam(0,12, token);

						mtLogOnInfo info;
						mtCredential crd; 

						m_pHost.UnBindStub(token,out info,out crd);

						ec = m_pCallback.OnLogOut(info,crd);

					}
					break;
				case ImtSMSServerCallback.SMSServerGetCredential:
					{
						mtCredential crd;

						if(m_pHost.RunEnv.Credential != null && m_pHost.RunEnv.Credential.Token == token)
                        {
							crd = m_pHost.RunEnv.Credential;

							sc = new MTSSerializer();

							sc.serialize(crd);

							ec = shErrorCode.sh_ec_Success;

						}
                        else
                        {
							ec = m_pCallback.OnGetCredential(token, out crd);

							if (ec == shErrorCode.sh_ec_Success)
							{
								sc = new MTSSerializer();

								sc.serialize(crd);
							}
						}

					}
					break;
				case ImtSMSServerCallback.SMSServerEventNotify:
					{
						var stub = m_pHost.GetStub(token);

						if (stub == null)
						{
							return shErrorCode.sh_ec_UserNotLogon;
						}

						uint nRank = 0, nEventId = 0, nEventSubId = 0;

						string describe = "";
						ulong ts = 0;

						mtLogOnInfo info = stub.LogOnInfo;
						mtCredential crd = stub.Credential;

						sc.serialize(ref nRank);
						sc.serialize(ref nEventId);
						sc.serialize(ref nEventSubId);
						sc.serialize(ref describe);
						sc.serialize(ref ts);

						ec = m_pCallback.OnEventNotify(nRank, info.StationType, info.ProgramType, crd.UserID, crd.UserName, info.StationName,info.ProgramName, nEventId, nEventSubId, describe, ts);

					}
					break;
				case ImtSMSServerCallback.SMSServerOperationLogNotify:
					{
						var stub = m_pHost.GetStub(token);

						if (stub == null)
						{
							return shErrorCode.sh_ec_UserNotLogon;
						}

						uint nRank = 0, nEventId = 0, nEventSubId = 0;

						ulong ts = 0;

						mtLogOnInfo info = stub.LogOnInfo;
						mtCredential crd = stub.Credential;

						mtOperationInfo oInfo = new mtOperationInfo();
						sc.serialize(ref nRank);
						sc.serialize(ref nEventId);
						sc.serialize(ref nEventSubId);
						sc.serialize(oInfo);
						sc.serialize(ref ts);
						ec = m_pCallback.OnOperationLogNotify(nRank, info.StationType, info.ProgramType, crd.UserID, crd.UserName, info.StationName, info.ProgramName, nEventId, nEventSubId, oInfo, ts);


					}
					break;
				case ImtSMSServerCallback.SMSServerErrorNotify:
					{
						uint nError = 0;

						sc.serialize(ref nError);

						ec = m_pCallback.OnErrorNotify(token, nError);

					}
					break;
				case ImtSMSServerCallback.SMSServerStatusNotify:
					{
						ulong nStatus = 0;

						sc.serialize(ref nStatus);

						ec = m_pCallback.OnStatusNotify(token, nStatus);
					}
					break;
				case ImtSMSServerCallback.SMSServerGetResourceString:
					{
						string lang = "";
						List<uint> listRid = new List<uint>();
						List<string> listResource;

						sc.serialize(ref lang);
						sc.serialize(ref listRid);

						ec = m_pCallback.OnGetResourceString(token, lang, listRid,out listResource);

						if (ec == shErrorCode.sh_ec_Success)
						{
							sc = new MTSSerializer();

							sc.serialize(ref listResource);
						}
					}
					break;

				case ImtSMSServerCallback.SMSServerReconComplete:
					{
						mtReconComplete info = new mtReconComplete();

						sc.serialize(info);

						ec = m_pCallback.OnReconComplete(token, info);
					}
					break;
				case ImtSMSServerCallback.SMSServerAutoStatResult:
					{
						mtAutoStatResult info = new mtAutoStatResult();

						sc.serialize(info);

						ec = m_pCallback.OnAutoStatResult(token, info);
					}
					break;
				case ImtSMSServerCallback.SMSServerPerformanceVerifResult:
					{
						mtVerifCalibResult info = new mtVerifCalibResult();

						sc.serialize(info);

						ec = m_pCallback.OnPerformanceVerifResult(token, info);
					}
					break;

				case ImtSMSServerCallback.SMSServerRecognizeComplete:
					{
						mtRecognizeComplete info = new mtRecognizeComplete();

						sc.serialize(info);

						ec = m_pCallback.OnRecognizeComplete(token, info);
					}
					break;
				case ImtSMSServerCallback.SMSServerNotifyRcgAlgInfo:
					{
						mtRecognizeAlgorithm info = new mtRecognizeAlgorithm();

						sc.serialize(info);

						ec = m_pCallback.OnNotifyRcgAlgInfo(token, info);
					}
					break;
				case ImtSMSServerCallback.SMSServerStorageComplete:
					{
						mtStorageComplete info = new mtStorageComplete();

						sc.serialize(info);

						ec = m_pCallback.OnStorageComplete(token, info);
					}
					break;
				case ImtSMSServerCallback.SMSServerDeleteImageResult:
					{
						mtDeleteImageResult deleteResult = new mtDeleteImageResult();
				
						sc.serialize(deleteResult);

						ec = m_pCallback.OnNotifyDeleteImageResult(token, deleteResult);

					}
					break;

				case ImtSMSServerCallback.SMSServerSaveImageResult:
					{
						mtSaveImageResult result = new mtSaveImageResult();

						sc.serialize(result);

						ec = m_pCallback.OnNotifySaveImageResult(token, result);

					}
					break;
				default:
                    return ec;
            }

            return ec;
        }
	}
}
